import axios from "axios"
import { BASE_URL } from "./config"
// 导入进度条
import Nprogress from "nprogress"
import "nprogress/nprogress.css"

class LLRequest {
  constructor(baseUrl) {
    this.instance = axios.create({
      baseURL: baseUrl
    })
    // 请求拦截
    this.instance.interceptors.request.use((config) => {
      Nprogress.start()
      const token = localStorage.getItem('token')
      if (token) {
        config.headers.authorization = token
      }
      return config
    }, (error) => {
      // 对请求错误做些什么
      return Promise.reject(error);
    })
    // 响应拦截
    this.instance.interceptors.response.use(response => {
      Nprogress.done()
      return response
    })
  }

  get(url, params) {
    return this.instance.request(url, { method: "GET", params })
  }

  post(url, data) {
    return this.instance.request(url, { method: "POST", data })
  }

  delete(url, data) {
    return this.instance.request(url, { method: "DELETE", data })
  }

  patch(url, data) {
    return this.instance.request(url, { method: "PATCH", data })
  }
}

const llRequest = new LLRequest(BASE_URL)

export {
  llRequest
}